//
//  WPSafeViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPSafeViewController.h"
#import "UserModelArchiver.h"
#import "ImageHandle.h"

@interface WPSafeViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) UITextField * nameTextField;/**< 昵称修改 */
@property (nonatomic, strong) UITableView * safeTableView;/**< 安全列表 */
@property (nonatomic, strong) NSArray * safeArr;/**< 安全列表数据源 */
@property (nonatomic, strong) UIButton * quitBtn;/**< 退出登录 */
@property (nonatomic, strong) NSMutableArray * nextVCArr;/**< 下一个界面控制器 */
@property (nonatomic, strong) NSArray * nextVCTitles;/**< 下一界面标题 */
@end

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";

@implementation WPSafeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.safeArr = @[@"账号密码", @"手机绑定", @"收货地址"];
    self.nextVCTitles = @[@"登录密码修改", @"手机号码更改", @"收货地址"];
    NSArray * nextvc = @[@"WPChangePwdViewController", @"WPPhoneViewController", @"WPAddressViewController"];
    self.nextVCArr = [NSMutableArray arrayWithCapacity:0];
    [nextvc enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [_nextVCArr addObject:[[NSClassFromString(obj) alloc]init]];
    }];
}
- (void)initializeUserInterface{
    self.navigationController.navigationBarHidden = NO;
    [self.BasicStatusBackView removeFromSuperview];
    [self.view addSubview:self.nameTextField];
    [self.view addSubview:self.safeTableView];
    if ([self.safeTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.safeTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.safeTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.safeTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    [self.view addSubview:self.quitBtn];
}
#pragma mark - responds events
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)respondsToQuitBtn {
    [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"AutoLogin"];
    [ImageHandle deleteImageWithImageName:@"headImage.png"];
    [UserModel defaultUser].phoneNumber = @"";
    [UserModel defaultUser].balance = @(0);
    [UserModel defaultUser].memberRankNumber = @(0);
    [UserModel defaultUser].userID = nil;
    [UserModelArchiver deleteUserData];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 刷新数据
- (void)refreshData {
}
#pragma mark - 协议UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _safeArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.text = @"";
    cell.textLabel.text = _safeArr[indexPath.row];
    if (indexPath.row == 1) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@****%@", [[UserModel defaultUser].phoneNumber substringToIndex:3], [[UserModel defaultUser].phoneNumber substringWithRange:NSMakeRange(7, 4)]];
;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark - UITableViewDelegate
//用户点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self pushToNextController:_nextVCArr[indexPath.row] withVCTitle:_nextVCTitles[indexPath.row]];
}

#pragma mark - getter
- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, navBar_Fheight + 5*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 40*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 120*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(15*DHFlexibleHorizontalMutiplier(), 0, 70*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            title.text = @"账户名称";
            [leftView addSubview:title];
            text.leftView = leftView;
            text.backgroundColor = [UIColor whiteColor];
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(text.bounds)-1, SCREEN_SIZE.width, 1)];
            line.backgroundColor = COLOR_RGB(231, 231, 231, 1);
            [text addSubview:line];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.placeholder = _nickName;
            text.delegate = self;
            text;
        });
    }
    return _nameTextField;
}
- (UITableView *)safeTableView {
    if (!_safeTableView) {
        _safeTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.nameTextField.frame), SCREEN_SIZE.width, SCREEN_SIZE.height - CGRectGetMaxY(self.nameTextField.frame)-CGRectGetHeight(self.tabBarController.tabBar.bounds)) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.bounces = NO;
            tableview.separatorColor = COLOR_RGB(211, 211, 211, 1);
            tableview.backgroundColor = [UIColor clearColor];
            tableview;
        });
    }
    return _safeTableView;
}
- (UIButton *)quitBtn {
    if (!_quitBtn) {
        _quitBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.height - 50*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"退出登录" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToQuitBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;

        });
    }
    return _quitBtn;
}
@end
